package org.toolbook.validator;

import org.toolbook.model.User;
import org.toolbook.render.PhotoCodeRender;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;
/**
 * 用户登陆的验证器
 * @author meigang
 * @date 2015-06-20 20:26
 *
 */
public class LoginValidator extends Validator{

	@Override
	protected void validate(Controller c) {
		validateRequired("username", "username_info", "用户名不能为空");
		validateRequired("password", "password_info", "密码不能为空");
		validateRequired("code", "code_info", "验证码不能为空");
		//验证验证码
		String code = c.getPara("code");
		if(!code.isEmpty()){
			if(!PhotoCodeRender.validate(c, code)){
				addError("code_info", "验证码输入不正确");
			}
		}
        //验证用户是否合法
        User user = User.dao.checkLoginUser(c.getPara("username"),c.getPara("password"));
        if(user == null){
        	addError("up_info", "用户名或者密码错误");
        }
	}

	@Override
	protected void handleError(Controller c) {
		// TODO Auto-generated method stub
		c.keepPara("username");
		c.keepPara("password");
		c.render("/index.jsp");
	}

}
